package com.tripspins.app.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.location.Address;

import java.util.Locale;

/**
 * Created by João.Rutkoski on 06/09/13.
 */
public class AddressDAO {


    private SQLiteDatabase database;

    public AddressDAO(SQLiteDatabase database)
    {
        this.database = database;
    }

    public void persist(long pinId, Address address) {

        if(getAddress(pinId)==null)
            insertAddress(pinId,address);
        else
            updateAddress(pinId);
    }



    public void insertAddress(long pinId, Address address)
    {
        StringBuilder sbInsert = new StringBuilder();
        sbInsert.append("INSERT INTO PIN_ADDRESS ( id_pin " );
        sbInsert.append(                         ",txt_country ");
        sbInsert.append(                         ",txt_locale ");
        sbInsert.append(                         ",txt_state ");
        sbInsert.append(                         ",txt_city ");
        sbInsert.append(                         ",txt_street ");
        sbInsert.append(                         ",num_number )");
        sbInsert.append(                 "VALUES ( ? ");//id_pin
        sbInsert.append(                        ", ? ");//txt_country
        sbInsert.append(                        ", ? ");//txt_locale
        sbInsert.append(                        ", ? ");//txt_state
        sbInsert.append(                        ", ? ");//txt_city
        sbInsert.append(                        ", ? ");//txt_street
        sbInsert.append(                        ", ? );");//num_number

        database.execSQL(sbInsert.toString(),new String[]{
                String.valueOf(pinId),
                address.getCountryName(),
                address.getLocale().toString(),
                address.getAdminArea(),
                address.getSubLocality(),
                address.getThoroughfare(),
                address.getSubThoroughfare()});
    }

    private void updateAddress(long pinId) {
        StringBuilder sb = new StringBuilder();
        sb.append("Update Address ");  //0
        sb.append(" set txt_synced = 'Y' ");   //1
        sb.append(" where  id_pin = ? ");    //2

        database.execSQL(sb.toString(),new String[]{String.valueOf(pinId)});
    }

    public Address getAddress(long pinId)  {


        StringBuilder sb = new StringBuilder();
        sb.append("Select txt_country ");  //0
        sb.append(     ", txt_locale ");   //1
        sb.append(     ", txt_state ");    //2
        sb.append(     ", txt_city ");     //3
        sb.append(     ", txt_street ");   //4
        sb.append(     ", num_number ");  //5
        sb.append( " From pin_address");
        sb.append( " Where id_pin = ?");

        Cursor cursorAddress = database.rawQuery(
                sb.toString(),
                new String[] { String.valueOf(pinId) });
        cursorAddress.moveToFirst();
        if (cursorAddress.isAfterLast()) return null;

        Address address = new Address(new Locale(cursorAddress.getString(1)));
        address.setCountryName( cursorAddress.getString(0) );
        address.setAdminArea(cursorAddress.getString(2));
        address.setSubLocality(cursorAddress.getString(3));
        address.setThoroughfare(cursorAddress.getString(4));
        address.setSubThoroughfare(cursorAddress.getString(5));
        cursorAddress.close();
        return address;



    }

    public Address getAddressToSync(long pinId) {
        StringBuilder sb = new StringBuilder();
        sb.append("Select txt_country ");  //0
        sb.append(     ", txt_locale ");   //1
        sb.append(     ", txt_state ");    //2
        sb.append(     ", txt_city ");     //3
        sb.append(     ", txt_street ");   //4
        sb.append(     ", num_number ");  //5
        sb.append( " From pin_address ");
        sb.append( " Where id_pin = ? ");
        sb.append( "   and txt_synced = 'N'");

        Cursor cursorAddress = database.rawQuery(
                sb.toString(),
                new String[] { String.valueOf(pinId) });
        cursorAddress.moveToFirst();
        if (cursorAddress.isAfterLast()) return null;

        Address address = new Address(new Locale(cursorAddress.getString(1)));
        address.setCountryName( cursorAddress.getString(0) );
        address.setAdminArea(cursorAddress.getString(2));
        address.setSubLocality(cursorAddress.getString(3));
        address.setThoroughfare(cursorAddress.getString(4));
        address.setSubThoroughfare(cursorAddress.getString(5));
        cursorAddress.close();
        return address;
    }

    public Address getAddressSynced(long pinId) {
        StringBuilder sb = new StringBuilder();
        sb.append("Select txt_country ");  //0
        sb.append(     ", txt_locale ");   //1
        sb.append(     ", txt_state ");    //2
        sb.append(     ", txt_city ");     //3
        sb.append(     ", txt_street ");   //4
        sb.append(     ", num_number ");  //5
        sb.append( " From pin_address ");
        sb.append( " Where id_pin = ? ");
        sb.append( "   and txt_synced = 'Y'");

        Cursor cursorAddress = database.rawQuery(
                sb.toString(),
                new String[] { String.valueOf(pinId) });
        cursorAddress.moveToFirst();
        if (cursorAddress.isAfterLast()) return null;

        Address address = new Address(new Locale(cursorAddress.getString(1)));
        address.setCountryName( cursorAddress.getString(0) );
        address.setAdminArea(cursorAddress.getString(2));
        address.setSubLocality(cursorAddress.getString(3));
        address.setThoroughfare(cursorAddress.getString(4));
        address.setSubThoroughfare(cursorAddress.getString(5));
        cursorAddress.close();
        return address;
    }
}
